package com.yonyou.gmmc.service.maintain.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.dms.http.HttpCommonUtil;
import org.marketing.common.dto.MQReceiveResult;
import org.marketing.common.event.BizEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.maintain.constant.MaintainConstant;
import com.yonyou.gmmc.service.maintain.dao.TtEventHandleLogMapper;
import com.yonyou.gmmc.service.maintain.dao.TtMaintenanceRecordMapper;
import com.yonyou.gmmc.service.maintain.dto.MaintainRecord;
import com.yonyou.gmmc.service.maintain.model.TtEventHandleLog;

@Service
@Transactional
public class MiantainMqService {
	private Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private TtEventHandleLogMapper ttEventHandleLogMapper;
	@Autowired
	private TtMaintenanceRecordMapper ttMaintenanceRecordMapper;

    @Value("${DCMS-url}")
    private String dcmsUrl;
	/**
	 * 维修履历同步
	 * 
	 * @auther qianjs163@163.com 2017年3月24日 上午10:11:43
	 * @param event
	 * @return
	 * @throws Exception 
	 */
	public MQReceiveResult addMaintainRecord(BizEvent event) throws Exception {
		logger.info("----addMaintainRecord---");
		MQReceiveResult result = new MQReceiveResult();
		logger.info("----BizEvent getEventData---"+event.getEventData());
		if (!queryEventNum(event)) {
			result.setResult("200");
			result.setMessage("重复的事件，不进行处理");// 返回的消息
			return result;
		}
		// 业务开始
		Map<String, Object> map = new HashMap<>();
		map.put("vin", event.getEventData().get("VIN"));
		String vin = event.getEventData().get("VIN").toString();
		//logger.info("VIN为："+map);

        String uri=MaintainConstant.CARINFO_URL.replaceAll("dcmshost", dcmsUrl);
		List<MaintainRecord> l = HttpCommonUtil.getOwnerInfo(map, MaintainConstant.DMS_MAINTAIN_ID,
		    uri, MaintainRecord.class);
		if (l!=null&&!l.isEmpty()) {
		    l.get(0).setVin(vin);
		    logger.info("Suggestion:"+l.get(0).getSuggestion());
		    
			ttMaintenanceRecordMapper.insertBatchMaintain(l);
		}
		result.setResult("200");
		result.setMessage(MaintainConstant.MAINTAIN_MQ_BACKINFO);// 返回的消息
		return result;
	}

	/**
	 * 发送预约信息接口
	 * 
	 * @auther qianjs163@163.com 2017年3月24日 上午10:11:17
	 * @param event
	 * @return
	 * @throws Exception 
	 */
	public MQReceiveResult sendApplyInfo(BizEvent event) throws Exception {
		logger.info("----sendApplyInfo---");
		MQReceiveResult result = new MQReceiveResult();
		if (!queryEventNum(event)) {
			result.setResult("200");
			result.setMessage("重复的事件，不进行处理");// 返回的消息
			return result;
		}
		// 业务开始
		Map<String, Object> map = event.getEventData();
		HttpCommonUtil.getOwnerInfo(map, MaintainConstant.DMS_MAINTAIN_ID,dcmsUrl, String.class);
		result.setResult("200");
		result.setMessage(MaintainConstant.MAINTAIN_MQ_BACKINFO);// 返回的消息
		return result;
	}

	/**
	 * 潜客留资信息接口
	 * 
	 * @auther qianjs163@163.com 2017年3月24日 上午10:11:17
	 * @param event
	 * @return
	 * @throws Exception 
	 */
	public MQReceiveResult sendUserInfo(BizEvent event) throws Exception {
		logger.info("----sendUserInfo---");
		MQReceiveResult result = new MQReceiveResult();
		if (!queryEventNum(event)) {
			result.setResult("200");
			result.setMessage("重复的事件，不进行处理");// 返回的消息
			return result;
		}
		// 业务开始
		Map<String, Object> map = event.getEventData();
		HttpCommonUtil.getOwnerInfo(map, MaintainConstant.DMS_USER_ID,dcmsUrl,String.class);
		result.setResult("200");
		result.setMessage(MaintainConstant.MAINTAIN_MQ_BACKINFO);// 返回的消息
		return result;
	}

	/**
	 * 推荐购车信息接口
	 * 
	 * @auther qianjs163@163.com 2017年3月24日 上午10:11:17
	 * @param event
	 * @return
	 * @throws Exception 
	 */
	public MQReceiveResult sendCarInfo(BizEvent event) throws Exception {
		logger.info("----sendCarInfo---");
		MQReceiveResult result = new MQReceiveResult();
		if (!queryEventNum(event)) {
			result.setResult("200");
			result.setMessage("重复的事件，不进行处理");// 返回的消息
			return result;
		}
		// 业务开始
		Map<String, Object> map = event.getEventData();
		HttpCommonUtil.getOwnerInfo(map, MaintainConstant.DMS_CAR_ID, dcmsUrl,String.class);
		result.setResult("200");
		result.setMessage(MaintainConstant.MAINTAIN_MQ_BACKINFO);// 返回的消息
		return result;
	}

	/**
	 * 新增事件处理记录
	 * 
	 * @param event
	 * @return
	 */
	public int insertHandleLog(BizEvent event) {
		TtEventHandleLog log = new TtEventHandleLog();
		log.setBcId(event.getEventBizType().getCodeLong());
		log.setTcId(event.getEventBizStep().getCodeLong());
		log.setEventKey(event.getEventKey());
		if (event.getEventData().toString().length() > 3500) {
			log.setEventData(event.getEventData().toString().substring(0, 3500));
		} else {
			log.setEventData(event.getEventData().toString());
		}
		log.setCreateBy(MaintainConstant.OPT_USER);
		log.setCreateDate(new Date());
		return ttEventHandleLogMapper.insertSelective(log);
	}

	/**
	 * 查询数量
	 * 
	 * @param event
	 * @return
	 */
	public boolean queryEventNum(BizEvent event) {
		TtEventHandleLog log = new TtEventHandleLog();
		log.setBcId(event.getEventBizType().getCodeLong());
		log.setTcId(event.getEventBizStep().getCodeLong());
		log.setEventKey(event.getEventKey());
		int i = ttEventHandleLogMapper.selectCount(log);
		if (i > 0) {
			return false;
		}
		return true;
	}

}
